#ifndef BLEDECODER_HPP
#define BLEDECODER_HPP

#include <stdint.h>
#include <functional>
#include "blepacket.hpp"


class bledecoder
{
public:
	bledecoder();
	~bledecoder();
	long long get_channel_freq(int channel_number);
	void set_callback(std::function<void(lell_packet)> callback);
	size_t receiver(int16_t* samples, size_t samples_len);
	void set_access_addr(uint64_t access_addr);
	void set_channel(uint8_t channel);
private:
	bool feedOne(const uint16_t sample);
	void RB_inc(void);
	uint8_t SwapBits(uint8_t a);
	bool Quantize(int16_t l);
	bool DecodePacket(int32_t sample, int srate);
	std::function<void(lell_packet)> callback;
	uint8_t get_channel_k(uint8_t ch);
	int32_t ExtractThreshold(void);
	bool DetectPreamble(void);
	bool DecodeBTLEPacket(int32_t sample, int srate);
	void ExtractBytes(int l, uint8_t* buffer, int count);
	uint8_t inline ExtractByte(int l);
	void btle_reverse_whiten(uint8_t chan, uint8_t* data, uint8_t len);
	uint32_t btle_reverse_crc(const uint8_t* data, uint8_t len, uint8_t* dst);
	int atan_lut_init();
	int polar_disc_lut(int ar, int aj, int br, int bj);
	void multiply(int ar, int aj, int br, int bj, int *cr, int *cj);
	int16_t pre_r, pre_j;
	uint8_t chan;
	float last_phase;
	int rb_head = -1;
	int skipSamples;
	int32_t samples;
	int g_srate;
	int srate;
	int32_t g_threshold;
	int16_t *rb_buf;
	uint64_t access_addr;
	

	int *atan_lut = nullptr;
	int atan_lut_size = 131072; /* 512 KB */
	int atan_lut_coef = 8;
};


#endif
